package com.myhkvideo.yulongbpcdemo.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.myhkvideo.yulongbpcdemo.entity.EffectDayInfoData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EffectDayInfoService {
    private static final String API_URL = "http://ylty.westmining.com:60016/api/BoardProductionCommand/EffectDayInfo";

    @Autowired
    private LoginService loginService;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ObjectMapper objectMapper;
    String currentDateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

    public ResponseEntity<String> getEffectDayInfo() {
        String token = loginService.getNewToken();
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        headers.add("Authorization", "Bearer " + token);
        HttpEntity<String> requestEntity = new HttpEntity<>(headers);
        return restTemplate.exchange(API_URL, HttpMethod.POST, requestEntity, String.class);
    }

    public List<EffectDayInfoData> getAndConvertEffectDayInfo() throws IOException, ParseException {
        ResponseEntity<String> response = getEffectDayInfo();
        Map<String, Object> responseBody = objectMapper.readValue(response.getBody(), Map.class);
        List<Map<String, Object>> dataList = (List<Map<String, Object>>) responseBody.get("Data");
        return convertToEffectDayInfoDataList(dataList);
    }

    private List<EffectDayInfoData> convertToEffectDayInfoDataList(List<Map<String, Object>> dataList) throws ParseException {
        List<EffectDayInfoData> resultList = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (Map<String, Object> dataMap : dataList) {
            EffectDayInfoData data = new EffectDayInfoData();
            data.setCreateTime(dateFormat.parse((String) dataMap.get("createtime")));
            data.setD_ycjone_cll(BigDecimal.valueOf((Double) dataMap.get("d_ycjone_cll")));
            data.setD_ycjone_sc(BigDecimal.valueOf((Double) dataMap.get("d_ycjone_sc")));
            data.setD_ycjone_tx(BigDecimal.valueOf((Double) dataMap.get("d_ycjone_tx")));
            data.setD_ycjtwo_cll(BigDecimal.valueOf((Double) dataMap.get("d_ycjtwo_cll")));
            data.setD_ycjtwo_sc(BigDecimal.valueOf((Double) dataMap.get("d_ycjtwo_sc")));
            data.setD_ycjtwo_tx(BigDecimal.valueOf((Double) dataMap.get("d_ycjtwo_tx")));
            data.setD_ycjthree_cll(BigDecimal.valueOf((Double) dataMap.get("d_ycjthree_cll")));
            data.setD_ycjthree_sc(BigDecimal.valueOf((Double) dataMap.get("d_ycjthree_sc")));
            data.setD_ycjthree_tx(BigDecimal.valueOf((Double) dataMap.get("d_ycjthree_tx")));
            data.setD_ycjfour_cll(BigDecimal.valueOf((Double) dataMap.get("d_ycjfour_cll")));
            data.setD_ycjfour_sc(BigDecimal.valueOf((Double) dataMap.get("d_ycjfour_sc")));
            data.setD_ycjfour_tx(BigDecimal.valueOf((Double) dataMap.get("d_ycjfour_tx")));
            data.setD_ecj_cll(BigDecimal.valueOf((Double) dataMap.get("d_ecj_cll")));
            data.setD_ecj_sc(BigDecimal.valueOf((Double) dataMap.get("d_ecj_sc")));
            data.setD_ecj_tx(BigDecimal.valueOf((Double) dataMap.get("d_ecj_tx")));
            data.setD_ylc_cll(BigDecimal.valueOf((Double) dataMap.get("d_ylc_cll")));
            data.setD_ylc_sc(BigDecimal.valueOf((Double) dataMap.get("d_ylc_sc")));
            data.setD_ylc_tx(BigDecimal.valueOf((Double) dataMap.get("d_ylc_tx")));
            resultList.add(data);
        }
        return resultList;
    }
}